package com.lx.javasunmmrize.tcp;

import java.io.*;
import java.net.Socket;

/**
 * @Description 测试socket客户端
 *
 *  1.创建一个socket实例
 *
 * @Author lx
 * @Date 2021/3/6 21:01
 **/
public class SocketClient {

    public static void main(String[] args) throws InterruptedException {
        try {
            // 和服务器创建连接
            Socket socket = new Socket("localhost",10001);//创建流套接字并将其连接到指定主机上的指定端口号。

            // 要发送给服务器的信息
            OutputStream os = socket.getOutputStream();//返回此套接字的输出流。
            PrintWriter pw = new PrintWriter(os);
            String clientInfo ="\"<?xml version=\\\"1.0\\\" encoding=\\\"GBK\\\"?>\\n\" +\n" +
                    "                \"<Response>\\n\" +\n" +
                    "                \"\\t<CustID>1300000428</CustID>\\n\" +\n" +
                    "                \"\\t<CompID>1100000324</CompID>\\n\" +\n" +
                    "                \"\\t<Items>\\n\" +\n" +
                    "                \"\\t\\t<Item>\\n\" +\n" +
                    "                \"\\t\\t\\t<Sku_ProductNo>sku_0004</Sku_ProductNo>\\n\" +\n" +
                    "                \"\\t\\t\\t<Wms_Code>1700386977</Wms_Code>\\n\" +\n" +
                    "                \"\\t\\t\\t<Sku_Response>T</Sku_Response>\\n\" +\n" +
                    "                \"\\t\\t\\t<Sku_Reason></Sku_Reason>\\n\" +\n" +
                    "                \"\\t\\t</Item>\\n\" +\n" +
                    "                \"\\t\\t<Item>\\n\" +\n" +
                    "                \"\\t\\t\\t<Sku_ProductNo>0005</Sku_ProductNo>\\n\" +\n" +
                    "                \"\\t\\t\\t<Wms_Code>1700386978</Wms_Code>\\n\" +\n" +
                    "                \"\\t\\t\\t<Sku_Response>T</Sku_Response>\\n\" +\n" +
                    "                \"\\t\\t\\t<Sku_Reason></Sku_Reason>\\n\" +\n" +
                    "                \"\\t\\t</Item>\\n\" +\n" +
                    "                \"\\t</Items>\\n\" +\n" +
                    "                \"\\t<xml>\\n\" +\n" +
                    "                \"\\t\\t<Item>\\n\" +\n" +
                    "                \"\\t\\t\\t<Sku_ProductNo>sku_0004</Sku_ProductNo>\\n\" +\n" +
                    "                \"\\t\\t\\t<Wms_Code tx=\\\"js\\\">1700386977</Wms_Code>\\n\" +\n" +
                    "                \"\\t\\t\\t<Sku_Response>T</Sku_Response>\\n\" +\n" +
                    "                \"\\t\\t\\t<Sku_Reason></Sku_Reason>\\n\" +\n" +
                    "                \"\\t\\t</Item>\\n\" +\n" +
                    "                \"\\t\\t<Item>\\n\" +\n" +
                    "                \"\\t\\t\\t<Sku_ProductNo>0005</Sku_ProductNo>\\n\" +\n" +
                    "                \"\\t\\t\\t<Wms_Code>1700386978</Wms_Code>\\n\" +\n" +
                    "                \"\\t\\t\\t<Sku_Response>T</Sku_Response>\\n\" +\n" +
                    "                \"\\t\\t\\t<Sku_Reason></Sku_Reason>\\n\" +\n" +
                    "                \"\\t\\t</Item>\\n\" +\n" +
                    "                \"\\t</xml>\\n\" +\n" +
                    "                \"</Response>\"";
//            byte[] bytes = clientInfo.getBytes();
            pw.write(clientInfo);
            pw.flush();

            socket.shutdownOutput();//禁用此套接字的输出流。 对于TCP套接字，任何先前写入的数据将被发送，随后是TCP的正常连接终止序列

            // 从服务器接收的信息
            InputStream is = socket.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String info = null;
            while((info = br.readLine())!=null){
                System.out.println("server:"+info);
            }

            br.close();
            is.close();
            os.close();
            pw.close();
            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
